Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

438
Visualizações
Async JS : The console log displays the data as text but the HTML DIV renders [Promise Object]

I have a dynamic resultArray numbers.The console.log(data) shows the data text I want from the API. But the div in the HTML returns a [Object Promise] .

let resultArray = ['6']

  const getUrl = (num) => `http://numbersapi.com/${num}`;
  const getFacts = async (num) => {
    try {
      const resp = await fetch(getUrl(num));
      const data = await resp.text();
      console.log(data);
      return data;
    } catch (error) {
      console.log(error);
    }
  };

let theQuote = document.getElementById("quote");
theQuote.textContent = getFacts(resultArray);  // [object promise]
<!DOCTYPE html>
<html lang="en">
  <head>
  </head>
  <body class="main">
    <div id="quote"></div>
  </body>
</html>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I don't sure but it may work for you.

(async () => {
    let theQuote = document.getElementById("quote");
theQuote.textContent = await getFacts(resultArray);
})();
about 4 years ago · Juan Pablo Isaza Relatório

0

cuz your function getFacts() return a promise and you need to resolve it when use it , try to render data inside the function to avoid this

let theQuote = document.getElementById("quote");
let resultArray = ['6']

  const getUrl = (num) => `http://numbersapi.com/${num}`;
  const getFacts = async (num) => {
    try {
      const resp = await fetch(getUrl(num));
      const data = await resp.text();
      console.log(data);
theQuote.textContent = data;  
    } catch (error) {
      console.log(error);
    }
  };
 
about 4 years ago · Juan Pablo Isaza Relatório

0

Basically you just need to wait until the promise from getFacts gets resolved. You can achieve that by using then method of the promise like below:

getFacts(resultArray).then((facts) => {
  let theQuote = document.getElementById("quote");
  theQuote.textContent = facts;
})
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda